package com.mustafaiev.tair.autobase.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mustafaiev.tair.autobase.db.DBConnector;
import com.mustafaiev.tair.autobase.garage.Car;

/**
 * 
 * Class implements {@link CarDao} Data Access Object
 * 
 * @author Tair Mustafaiev
 * 
 */
public class CarDaoImpl implements CarDao {

    @Override
    public int addCar(Car car) throws SQLException {
        Connection connect = null;
        PreparedStatement statement = null;

        try {
            connect = DBConnector.getInstance().getConnection();
            statement = connect.prepareStatement("INSERT INTO " + getTableName()
                    + " (ID, DRIVER_ID, MAKER, MODEL, OPTIONS, IN_ACTION) "
                    + "VALUES (?, ?, ?, ?, ?, ?)");
            // set car id
            statement.setNull(1, java.sql.Types.BIGINT);
            // set driver id
            statement.setInt(2, car.getDriverId());
            // set car maker
            statement.setString(3, car.getMaker());
            // set car model
            statement.setString(4, car.getModel());
            // set car options
            statement.setString(5, car.getOptions());
            // set car usability
            statement.setInt(6, car.getInAction());

            // execute statement
            return statement.executeUpdate();

        } finally {
            DBConnector.getInstance().close(connect);
            DBConnector.getInstance().close(statement);
        }

    }

    @Override
    public ResultSet getCar(Car car) throws SQLException {

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            connection = DBConnector.getInstance().getConnection();
            statement = connection.prepareStatement("SELECT * FROM " + getTableName()
                    + " AS C WHERE C.ID = ?");

            // set car identificator
            statement.setInt(1, car.getId());
            // execute query
            result = statement.executeQuery();

        } finally {
            DBConnector.getInstance().close(connection);
            DBConnector.getInstance().close(result);
            DBConnector.getInstance().close(statement);
        }
        return result;
    }

    @Override
    public ResultSet getCarsList() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            connection = DBConnector.getInstance().getConnection();
            statement = connection.prepareStatement("SELECT * FROM " + getTableName());
            // execute query
            result = statement.executeQuery();

        } finally {
            DBConnector.getInstance().close(connection);
            DBConnector.getInstance().close(result);
            DBConnector.getInstance().close(statement);
        }
        return result;
    }

    @Override
    public int deleteCar(Car car) throws SQLException {

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBConnector.getInstance().getConnection();
            statement = connection.prepareStatement("DELETE * FROM " + getTableName()
                    + " AS C WHERE C.ID = ?");

            // set car identificator
            statement.setInt(1, car.getId());
            // execute query
            return statement.executeUpdate();

        } finally {
            DBConnector.getInstance().close(connection);
            DBConnector.getInstance().close(statement);
        }
    }

    @Override
    public int updateCarInfo(Car car) throws SQLException {

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBConnector.getInstance().getConnection();
            statement = connection.prepareStatement("UPDATE " + getTableName()
                    + " AS C SET AU.DRIVER_ID = ?, AU.MAKER = ?, AU.MODEL = ?, "
                    + " AU.OPTIONS = ?, AU.IN_ACTION = ? WHERE C.ID = ?");

            // update car driver identificator
            statement.setInt(1, car.getDriverId());
            // update car maker
            statement.setString(2, car.getMaker());
            // update car model
            statement.setString(3, car.getModel());
            // update car options
            statement.setString(4, car.getOptions());
            // update car in_action attribute
            statement.setInt(5, car.getInAction());
            // set car id
            statement.setInt(6, car.getId());

            // execute query
            return statement.executeUpdate();

        } finally {
            DBConnector.getInstance().close(connection);
            DBConnector.getInstance().close(statement);
        }
    }

    /**
     * @return String table name
     */
    private static String getTableName() {
        return "AUT_CARS";
    }

}
